/*
 * -------------------------------------------------------------------------- *
 * This is a Practice Work from the 'Algorithms, Part-I' course form Coursera.
 * 
 * -------------------------------------------------------------------------- *
 * Date:	29-Aug-2013 9:13:12 am
 */
package com.gs.coursera.algorithms.core.uf;

/**
 * @author Sabuj Das | sabuj.das@gmail.com
 *
 */
public class WeightedQuickUnion extends QuickUnion {

	private int[] sz;
	
	public WeightedQuickUnion(int n) {
		super(n);
		sz = new int[n];
		for (int i = 0; i < sz.length; i++) {
			sz[i] = 1;
		}
	}

	@Override
	public void union(int p, int q) {
		int i = root(p);
		int j = root(q);
		if(sz[i] < sz[j]){
			getNodes()[i] = j;
			sz[j] += sz[i];
		} else {
			getNodes()[j] = i;
			sz[i] += sz[j];
		}
	}

	
}
